package com.example.tse.rbhttpservice.service.common;


import android.content.Context;

import com.example.tse.rbhttpservice.service.HttpService;
import com.example.tse.rbhttpservice.service.RemoteServiceListenerT;
import com.example.tse.rbhttpservice.service.ResponseServiceListenerT;
import com.example.tse.rbhttpservice.service.RudderResponse;
import com.example.tse.rbhttpservice.service.common.model.CommonGetADList;
import com.example.tse.rbhttpservice.service.common.model.CommonGetADListRequest;
import com.example.tse.rbhttpservice.service.common.model.CommonGetADListResponse;
import com.example.tse.rbhttpservice.service.common.model.CommonGetRegionList;
import com.example.tse.rbhttpservice.service.common.model.CommonGetRegionListRequest;
import com.example.tse.rbhttpservice.service.common.model.CommonGetRegionListResponse;
import com.example.tse.rbhttpservice.service.common.model.CommonGetStatusRequest;
import com.example.tse.rbhttpservice.service.common.model.CommonGetStatusResponse;
import com.example.tse.rbhttpservice.service.common.model.CommonShareGoodsRequest;
import com.example.tse.rbhttpservice.service.common.model.CommonShareGoodsResponse;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.kymjs.kjframe.http.HttpParams;

import java.lang.reflect.Type;
import java.util.List;





/**
 *
 * @author wliu
 *    edi  tse
 */
public class CommonServiceImpl implements CommonService {
	private Context context;
	private static Gson gson = new Gson();


	public CommonServiceImpl(Context context) {
		this.context = context;
	}
	/**
	 * 获取某位置的广告，用于可能需要的地方（首页除外）
	 * @param request
	 * @param listener
	 * @return CommonGetADListResponse
	 */

	    public void getADList(CommonGetADListRequest request, final RemoteServiceListenerT< CommonGetADListResponse> listener) {

        final HttpParams params = new HttpParams();


            params.put("position",request.getPosition());
            params.put("virtualCatId",request.getVirtualCatId());

        final String url = "common/getADList";



        /**
        common
        getADList
        GetADList
        获取某位置的广告，用于可能需要的地方（首页除外）
        GET
        Common
        CommonGetADList
        common/getADList
        array
        */

           final Type type = new TypeToken<List<CommonGetADList>>() {
        }.getType();



            new HttpService().getList(context, url, params, CommonGetADListResponse.class, new ResponseServiceListenerT<RudderResponse>() {

             @Override
             public void onPreStart() {
                 super.onPreStart();
                 listener.PreStart();
             }

             @Override
             public void ok(RudderResponse rudderResponse) {
                 super.ok(rudderResponse);
                 List<CommonGetADList> items = gson.fromJson(rudderResponse.getBody(), type);
                 CommonGetADListResponse responce = new CommonGetADListResponse();
                 responce.setGetADList(items);
                 listener.ok(responce);
             }

             @Override
             public void okData(RudderResponse rudderResponse) {
                 super.okData(rudderResponse);

                 if (rudderResponse == null) {
                     listener.okData(new CommonGetADListResponse());
                 } else {
                     List<CommonGetADList> items = gson.fromJson(rudderResponse.getBody(), type);
                     CommonGetADListResponse responce = new CommonGetADListResponse();
                     responce.setGetADList(items);
                     listener.okData(rudderResponse == null ? new CommonGetADListResponse() : responce);
                 }
             }

             @Override
             public void okData(RudderResponse rudderResponse, int fromWhere) {
                 super.okData(rudderResponse, fromWhere);


                 if (rudderResponse == null) {
                     listener.okData(new CommonGetADListResponse(), fromWhere);
                 } else {
                     List<CommonGetADList> items = gson.fromJson(rudderResponse.getBody(), type);
                     CommonGetADListResponse responce = new CommonGetADListResponse();
                     responce.setGetADList(items);
                     listener.okData(rudderResponse == null ? new CommonGetADListResponse() : responce, fromWhere);
                 }


             }

             @Override
             public void onFailure(int errorNo, String strMsg) {
                 super.onFailure(errorNo, strMsg);
                 listener.failue(errorNo, strMsg);
             }

             @Override
             public void onFinish() {
                 super.onFinish();
                 listener.Finish();
             }

             @Override
             public void onMessage(String message) {
                 super.onMessage(message);
                 listener.onMessage(message);
             }


         });
    }






	/**
	 * 获取省市区，默认获取省列表，
	 * @param request
	 * @param listener
	 * @return CommonGetRegionListResponse
	 */

	    public void getRegionList(CommonGetRegionListRequest request, final RemoteServiceListenerT< CommonGetRegionListResponse> listener) {

        final HttpParams params = new HttpParams();


            params.put("regionId",request.getRegionId());
            params.put("type",request.getType());

        final String url = "common/getRegionList";



        /**
        common
        getRegionList
        GetRegionList
        获取省市区，默认获取省列表，
        GET
        Common
        CommonGetRegionList
        common/getRegionList
        array
        */

           final Type type = new TypeToken<List<CommonGetRegionList>>() {
        }.getType();



            new HttpService().getList(context, url, params, CommonGetRegionListResponse.class, new ResponseServiceListenerT<RudderResponse>() {

             @Override
             public void onPreStart() {
                 super.onPreStart();
                 listener.PreStart();
             }

             @Override
             public void ok(RudderResponse rudderResponse) {
                 super.ok(rudderResponse);
                 List<CommonGetRegionList> items = gson.fromJson(rudderResponse.getBody(), type);
                 CommonGetRegionListResponse responce = new CommonGetRegionListResponse();
                 responce.setGetRegionList(items);
                 listener.ok(responce);
             }

             @Override
             public void okData(RudderResponse rudderResponse) {
                 super.okData(rudderResponse);

                 if (rudderResponse == null) {
                     listener.okData(new CommonGetRegionListResponse());
                 } else {
                     List<CommonGetRegionList> items = gson.fromJson(rudderResponse.getBody(), type);
                     CommonGetRegionListResponse responce = new CommonGetRegionListResponse();
                     responce.setGetRegionList(items);
                     listener.okData(rudderResponse == null ? new CommonGetRegionListResponse() : responce);
                 }
             }

             @Override
             public void okData(RudderResponse rudderResponse, int fromWhere) {
                 super.okData(rudderResponse, fromWhere);


                 if (rudderResponse == null) {
                     listener.okData(new CommonGetRegionListResponse(), fromWhere);
                 } else {
                     List<CommonGetRegionList> items = gson.fromJson(rudderResponse.getBody(), type);
                     CommonGetRegionListResponse responce = new CommonGetRegionListResponse();
                     responce.setGetRegionList(items);
                     listener.okData(rudderResponse == null ? new CommonGetRegionListResponse() : responce, fromWhere);
                 }


             }

             @Override
             public void onFailure(int errorNo, String strMsg) {
                 super.onFailure(errorNo, strMsg);
                 listener.failue(errorNo, strMsg);
             }

             @Override
             public void onFinish() {
                 super.onFinish();
                 listener.Finish();
             }

             @Override
             public void onMessage(String message) {
                 super.onMessage(message);
                 listener.onMessage(message);
             }


         });
    }






	/**
	 * 获取统计状态，包括购物车状态（0购物车没有数据1购物车有数据）、个人中心中是否有更新状态（统计我的关注＋我的粉丝数量，大于0显示否则不显示）
	 * @param request
	 * @param listener
	 * @return CommonGetStatusResponse
	 */

	    public void getStatus(CommonGetStatusRequest request, final RemoteServiceListenerT< CommonGetStatusResponse> listener) {

        final HttpParams params = new HttpParams();



        final String url = "common/getStatus";


            new HttpService().get(context, url, params, CommonGetStatusResponse.class, new ResponseServiceListenerT<RudderResponse>() {

            @Override
            public void onPreStart() {
                super.onPreStart();
                listener.PreStart();
            }

            public void ok(RudderResponse rudderResponse) {
                super.ok(rudderResponse);
                listener.ok((CommonGetStatusResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse) {
                super.okData(rudderResponse);
                listener.okData(rudderResponse == null ? new CommonGetStatusResponse() : (CommonGetStatusResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse, int fromWhere) {
                super.okData(rudderResponse, fromWhere);
                listener.okData(rudderResponse == null ? new CommonGetStatusResponse() : (CommonGetStatusResponse) rudderResponse, fromWhere);
            }

            @Override
            public void onFailure(int errorNo, String strMsg) {
                super.onFailure(errorNo, strMsg);
                listener.failue(errorNo, strMsg);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                listener.Finish();
            }

            @Override
            public void onMessage(String message) {
                super.onMessage(message);
                listener.onMessage(message);
            }
        });
    }




	/**
	 * APP中分享前调用该接口，生成需要的分享商品页面
	 * @param request
	 * @param listener
	 * @return CommonShareGoodsResponse
	 */

	    public void shareGoods(CommonShareGoodsRequest request, final RemoteServiceListenerT< CommonShareGoodsResponse> listener) {

        final HttpParams params = new HttpParams();


            params.put("prodctId",request.getProdctId());

        final String url = "common/shareGoods";


            new HttpService().get(context, url, params, CommonShareGoodsResponse.class, new ResponseServiceListenerT<RudderResponse>() {

            @Override
            public void onPreStart() {
                super.onPreStart();
                listener.PreStart();
            }

            public void ok(RudderResponse rudderResponse) {
                super.ok(rudderResponse);
                listener.ok((CommonShareGoodsResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse) {
                super.okData(rudderResponse);
                listener.okData(rudderResponse == null ? new CommonShareGoodsResponse() : (CommonShareGoodsResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse, int fromWhere) {
                super.okData(rudderResponse, fromWhere);
                listener.okData(rudderResponse == null ? new CommonShareGoodsResponse() : (CommonShareGoodsResponse) rudderResponse, fromWhere);
            }

            @Override
            public void onFailure(int errorNo, String strMsg) {
                super.onFailure(errorNo, strMsg);
                listener.failue(errorNo, strMsg);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                listener.Finish();
            }

            @Override
            public void onMessage(String message) {
                super.onMessage(message);
                listener.onMessage(message);
            }
        });
    }




}